//
//  GRRiceOrderListVC.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/9/7.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRRiceOrderListVC.h"
#import "GRRiceOrderTableViewCell.h"
#import "GRStoreAction.h"
#import "GRRiceOrderModel.h"
#import "GRRiceOrderDetailVCViewController.h"
#import "GRSubmitOrderVC.h"
#import "GROrderCommentViewController.h"
#import "GRReturnReasonView.h"
#import "GRBuyDiscountSuccessVC.h"
#import "GRBlankSpace.h"
@interface GRRiceOrderListVC ()<GRRiceOrderTableViewCellDelegate,GRReturnReasonViewDelegate>
{
    NSInteger currentPage;
}

@property(nonatomic,strong)NSMutableArray * orderArr;
@property(nonatomic,strong)NSArray * cancelReasonArr;
@property(nonatomic,strong)GRBlankSpace * blankSpceView;

@end

@implementation GRRiceOrderListVC
-(GRBlankSpace *)blankSpceView{
    if (!_blankSpceView) {
        _blankSpceView = [GRBlankSpace xx_instantiateFromNib];
        _blankSpceView.contentMode = GRBlankContentModeEmptyOrders;
        _blankSpceView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        [self.view addSubview:_blankSpceView];
    }
    return _blankSpceView;
}
-(NSMutableArray *)orderArr{
    if (!_orderArr) {
        _orderArr = [NSMutableArray array];
    }
    return _orderArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.tableView registerNib:[UINib nibWithNibName:@"GRRiceOrderTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"riceCell"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadData) name:@"paySuucess" object:nil];
    if (_orderType == RiceOrderUnComment) {
        self.navigationItem.title = @"待评价";
    }
    WEAKSELF
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        currentPage = 1 ;
        [weakSelf requestListWithfreshType:GRRefreshForHeader];
        
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        currentPage++;
        [weakSelf requestListWithfreshType:GRRefreshForFooter];
    }];
    [self.tableView.mj_header beginRefreshing];
}
-(void)loadData{
    [self.tableView.mj_header beginRefreshing];
}
-(void)requestListWithfreshType:(GRRefresh)freshType{
    NSString * state;
    switch (_orderType) {
        case RiceOrderTypeAll:
            state = @"all";
            break;
        case RiceOrderTypeWaitPay:
            state = @"pay";
            break;
        case RiceOrderTypeCanUse:
            state = @"use";
            break;
        case RiceOrderTypeUsed:
            state = @"complete";
            break;
        case RiceOrderTypeReturn:
            state = @"refundAndExpired";
            break;
            
        case RiceOrderUnComment:
            state = @"comment";
            break;
        default:
            break;
    }
            NSDictionary * dic = @{
                                   @"currentPage":@(currentPage),
                                   @"pageSize":@(10),
                                   @"state":state
                                   };
            [GRStoreAction getRiceOrderList:dic ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
                if (success) {
                    NSLog(@"%@",responseObject);
                    
                    self.cancelReasonArr = responseObject[@"cancelReason"];
                    NSArray * goodlistArr = responseObject[@"dataList"];
                    if (freshType == GRRefreshForHeader) {
                        [self.orderArr removeAllObjects];
                    }
                    for (NSDictionary * dic  in goodlistArr) {
                        GRRiceOrderModel * riceModel = [GRRiceOrderModel yy_modelWithDictionary:dic];
                        [self.orderArr addObject:riceModel];
                    }
                    [self.tableView reloadData];
                    self.blankSpceView.hidden = self.orderArr.count <= 0?NO:YES;
                    
                }else {
                    [GRHUD showMessage:error.errorMsg atView:self.view];
                }
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            }];
    
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GRRiceOrderTableViewCell * orderCell = [tableView dequeueReusableCellWithIdentifier:@"riceCell" forIndexPath:indexPath];
    orderCell.delegate = self;
    orderCell.orderModel = self.orderArr[indexPath.row];
    return orderCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GRRiceOrderDetailVCViewController * orderDetailVC = [[GRRiceOrderDetailVCViewController alloc]init];
    GRRiceOrderModel * orderModel = self.orderArr[indexPath.row];
    orderDetailVC.orderId = orderModel.id;
    [self.navigationController pushViewController:orderDetailVC animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderArr.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    GRRiceOrderModel * orderModel = self.orderArr[indexPath.row];
    if ([orderModel.state isEqualToString:@"pay"]) {
        return 195;
    }else{
        if ([orderModel.state isEqualToString:@"cancel"]) {
            return 160;
        }else{
            if ([orderModel.state isEqualToString:@"use"]) {
                return 240;
            }else{
              return 190;
            }
        }
    }
}

-(void)detailOrderWithOrderType:(RiceOrderActionType)orderType orderModel:(GRRiceOrderModel *)orderModel{
    if (orderType == RiceOrderActionTypeCustomerService) {
        [[GRUMengCommon shareUMeng] enterAppletChat:self];
    }else if (orderType == RiceOrderActionTypeComment){
        GROrderCommentViewController * commentVC = [[GROrderCommentViewController alloc]init];
        commentVC.isRiceOrder = YES;
        commentVC.riceModel = orderModel;
        [self.navigationController pushViewController:commentVC animated:YES];
        
    }else if (orderType == RiceOrderActionTypeReturnMoeny){
        [GRReturnReasonView showGoodView:self DataSource:[NSMutableArray arrayWithArray:self.cancelReasonArr] OrderId:orderModel.id IsRiceOrder:YES IsCancle:NO];
        
    }else if (orderType == RiceOrderActionTypeCancleOrder){
        
           [GRReturnReasonView showGoodView:self DataSource:[NSMutableArray arrayWithArray:self.cancelReasonArr] OrderId:orderModel.id IsRiceOrder:YES IsCancle:YES];
    }else{//支付
        GRSubmitOrderVC * submitVC = [[GRSubmitOrderVC alloc]init];
        submitVC.orderId = orderModel.id;
        submitVC.isOrder = YES;
        [self.navigationController pushViewController:submitVC animated:YES];
    }
}

-(void)submitCancleReason:(NSString *)reson OrderId:(NSInteger)orderId sCancle:(BOOL)isCancle{//申请退款
    NSDictionary * dic = @{
                           @"cancelReason":reson,
                           @"id":@(orderId)
                           };
    
    if (isCancle) {
        [GRStoreAction RiceCancleOrder:dic ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
            if (success) {
                GRBuyDiscountSuccessVC *successVC = [[GRBuyDiscountSuccessVC alloc]init];
                successVC.type = BuyDiscountTypeCancleOrder;
                 successVC.orderId = orderId;
                [self.navigationController pushViewController:successVC animated:YES];
                [self.tableView.mj_header beginRefreshing];
            }
        }];
    }else{
 
    [GRStoreAction RiceOrderReturnMoney:dic ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            GRBuyDiscountSuccessVC *successVC = [[GRBuyDiscountSuccessVC alloc]init];
            successVC.type = BuyDiscountTypeReturnMoeny;
             successVC.orderId = orderId;
            [self.navigationController pushViewController:successVC animated:YES];
            [self.tableView.mj_header beginRefreshing];
        }
    }];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
